package com.pan.admin.module.loanConfig.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.loanConfig.entity.LoanFormFieldValue;
import com.pan.admin.module.loanConfig.service.LoanFormFieldValueService;
import com.pan.admin.module.loanProduct.entity.LoanProductPlanField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 动态单字段值Controller
 *
 * @author ghb
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/loanFrom/value")
public class LoanFormFieldValueController extends BaseController {
    @Autowired
    private LoanFormFieldValueService loanFormFieldValueService;

    /**
     * 查询动态单字段值列表
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:value:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanFormFieldValue loanFormFieldValue) {
        startPage();
        List<LoanFormFieldValue> list = loanFormFieldValueService.list(loanFormFieldValue);
        return getDataTable(list);
    }

    /**
     * 获取动态单字段值详细信息
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:value:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanFormFieldValueService.getById(id));
    }

    /**
     * 新增动态单字段值
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:value:add')")
    @Log(title = "动态单字段值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanFormFieldValue loanFormFieldValue) {
        return toAjax(loanFormFieldValueService.insertLoanFormFieldValue(loanFormFieldValue));
    }

    /**
     * 修改动态单字段值
     */
    @PreAuthorize("@ss.hasPermi('loanFrom:value:edit')")
    @Log(title = "动态单字段值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanFormFieldValue loanFormFieldValue) {
        if (loanFormFieldValue.getStatus() == 0) {
            LoanProductPlanField productField = new LoanProductPlanField();
            Long id = loanFormFieldValue.getId();
            int intValue = id.intValue();
            productField.setLoanFormFieldValueId(intValue);
            int count = loanFormFieldValueService.countFormProduct(productField);
            if (count > 0) {
                return AjaxResult.success("505", "该字段已关联机构，请取消关联后操作");
            }
        }
        return toAjax(loanFormFieldValueService.updateLoanFormFieldValue(loanFormFieldValue));
    }

}
